package com.ashin.recursion;

import org.junit.Test;

import java.util.*;

/**
 * 递归练习题
 * 1.求数组中的最大数
 * 2.1+2+3+...+n
 * 3.求n个整数的积
 * 4.求n个整数的平均值
 * 5.求n个自然数的最大公约数与最小公倍数
 * 6.有一对雌雄兔，每两个月就繁殖雌雄各一对兔子.问n个月后共有多少对兔子
 * 7.已知：数列1,1,2,4,7,13,24,44,...求数列的第 n项.
 *
 * @author Ashin
 */
public class Main {

    @Test
    public void testMaxOfArray() {
        int[] a = {2, 3, 34, 55678, 8, 1222};
        System.out.println(max(a));
    }


    /**
     * 求数组中的最大数
     *
     * @param a
     * @return
     */
    private int max(int[] a) {
        if (a.length == 0) {
            return 0;
        }
        return Math.max(a[0], max(Arrays.copyOfRange(a, 1, a.length)));
    }

    @Test
    public void testSum() {
        System.out.println(sum(18807));
    }

    /**
     * 1+2+3+...+n
     *
     * @param n
     * @return
     */
    private int sum(int n) {
        if (n == 0) {
            return n;
        }

        return sum(n - 1) + n;
    }

    @Test
    public void testSumRabbit() {
        System.out.println(rabbitNum(5, 1));
    }

    private int rabbitNum(int n, int rabbitNum) {
        if (n == 0 || n == 1) {
            return rabbitNum;
        }

        n -= 2;
        rabbitNum *= 2;


        return rabbitNum(n, rabbitNum);
    }




}
